package com.s.core.root;

import lombok.Getter;

/**
 * @author zhangjiangnan
 */
public class CodeFeature {
    public static boolean isSt(String name) {
        return name.contains(CODE_FEATURE_DESC.ST.getCodeFeature()) ||
                name.contains(CODE_FEATURE_DESC.S.getCodeFeature());
    }

    public static boolean isToDel(String name) {
        return name.startsWith(CODE_FEATURE_DESC.TO_DEL.getCodeFeature()) ||
                name.endsWith(CODE_FEATURE_DESC.TO_DEL.getCodeFeature());
    }

    public static CM_TYPE getCmType(String code) {
        if (code.startsWith(CODE_FEATURE_DESC._30.codeFeature) ||
                code.startsWith(CODE_FEATURE_DESC._68.codeFeature)) {
            return CM_TYPE._20;
        } else if (code.startsWith(CODE_FEATURE_DESC._8.codeFeature) ||
                code.startsWith(CODE_FEATURE_DESC._4.codeFeature) ||
                code.startsWith(CODE_FEATURE_DESC._920.codeFeature)) {
            return CM_TYPE._30;
        } else {
            return CM_TYPE._10;
        }
    }

    public static boolean isCanBuy(String code) {
        if (getCmType(code) == CM_TYPE._30) {
            return false;
        }
        return !code.startsWith(CODE_FEATURE_DESC._68.codeFeature);
    }

    public enum CM_TYPE {
        _10,
        _20,
        _30,
    }

    private enum CODE_FEATURE_DESC {
        _6("6", "上证"),
        _8("8", "北交所(83/87普通股票.82优选股票.88公开发行的股票.830新三板精选层)"),
        _4("4", "新三板(400老三板的A股股票,沪深市场退市.420老三板的B股股票,沪深市场退市.430市商改革前的股票代码,相当于老的新三板股票)"),
        _920("920", "北交所920号段"),
        _68("68", "科创板"),
        _30("30", "创业板"),
        ST("ST", "ST"),
        S("S", "未股改(5cm)"),
        TO_DEL("退", "待退市");

        @Getter
        private String codeFeature;
        private String desc;

        CODE_FEATURE_DESC(String codeFeature, String desc) {
            this.codeFeature = codeFeature;
            this.desc = desc;
        }
    }

    public static boolean isCqName(String name) {
        return name.startsWith("XD") || name.startsWith("XR") || name.startsWith("DR");
    }

    public static String getTsCode(String code) {
        if (code.startsWith(CODE_FEATURE_DESC._8.codeFeature) ||
                code.startsWith(CODE_FEATURE_DESC._4.codeFeature) ||
                code.startsWith(CODE_FEATURE_DESC._920.codeFeature)) {
            return code + ".BJ";
        } else if (code.startsWith(CODE_FEATURE_DESC._6.codeFeature)) {
            return code + ".SH";
        } else {
            return code + ".SZ";
        }
    }
}
